<template>
    <div>
        <div>
            <el-input
                    size="small"
                    class="addPosInput"
                    placeholder="添加二级学院..."
                    prefix-icon="el-icon-plus"
                    v-model="institute.instituteName"
                    @keydown.enter.native="addInstitute">
            </el-input>
            <el-button icon="el-icon-plus" size="small" type="primary" @click="addInstitute">添加</el-button>
        </div>
        <div class="posManaMain">
            <el-table
                    @selection-change="handleSelectionChange"
                    :data="institutes"
                    border
                    stripe
                    size="small"
                    style="width: 100%">
                <el-table-column
                        align="center"
                        type="selection"
                        width="120">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="id"
                        label="编号"
                        width="180">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="instituteName"
                        label="二级学院名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        align="center"
                        width="180"
                        prop="createDate"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        align="center"
                        width="180"
                        label="是否启用">
                    <template slot-scope="scope" style="align-items: center">
                        <el-tag size="small" type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag size="small" type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        width="240"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)" type="primary">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button @click="deleteMany" type="danger" size="small" style="margin-top: 8px" :disabled="multipleSelection.length === 0">
                批量删除
            </el-button>
        </div>
        <el-dialog
                title="修改二级学院"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <div>
                    <el-tag>二级学院名称</el-tag>
                    <el-input class="updatePosInput" size="small" v-model="updateInstitute.instituteName"></el-input>
                </div>
                <div style="margin-top: 8px">
                    <el-tag>是否启用</el-tag>
                    <el-switch
                            style="margin-left: 8px"
                            v-model="updateInstitute.enabled"
                            active-text="启用"
                            inactive-text="禁用">
                    </el-switch>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return{
                institute: {
                    instituteName: ''
                },
                /*修改用的表单封装*/
                updateInstitute: {
                    instituteName: '',
                    enabled: false
                },
                institutes: [],
                dialogVisible: false,
                multipleSelection: []
            }
        },
        mounted() {
              this.initInstitute()
        },
        methods: {
                initInstitute() {
                    this.getRequest("/system/institute/").then(res => {
                        if (res) {
                            console.log(res)
                            this.institutes = res
                        }
                    })
                },
            addInstitute() {
                /*输入了值 有值才能添加*/
                if (this.institute.instituteName) {
                    this.postRequest('/system/institute/', this.institute).then(res => {
                        if (res) {
                            /*刷新*/
                            this.initInstitute()
                            /*清空*/
                            this.institute.instituteName = ''
                        }
                    })
                } else {
                    this.$message.error('二级学院名称不能未空!');
                }
            },
            /*scope.$index是下标 scope.row是对应行数据*/
            showEditView(index, data) {
                this.dialogVisible = true
                //this.updatePos = data
                /*拷贝 避免出现编辑时点击取消 表格中显示的数据不会跟着变化 如果是this.updatePos = data
                    则会出现编辑时输入的数据跟表格中属性同时变化 若点击取消表格中数据没有变为原来的数据*/
                Object.assign(this.updateInstitute, data)
            },
            doUpdate() {
                this.putRequest('/system/institute/', this.updateInstitute).then(res => {
                    if (res) {
                        /*刷新*/
                        this.initInstitute()
                        /*清空输入框的值*/
                        this.updateInstitute.instituteName = ''
                        this.dialogVisible = false
                    }
                })
            },
            handleDelete(index, data) {
                this.$confirm('此操作将永久删除' + data.instituteName + ', 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/institute/" + data.id).then(res => {
                        if(res) {
                            /*刷新*/
                            this.initInstitute()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {
                /*multipleSelection保存的是一组的选项*/
                this.multipleSelection = val
                console.log(val)
            },
            deleteMany() {
                this.$confirm('此操作将永久删除这' + this.multipleSelection.length + '条记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids ='?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids=' + item.id + '&' /*后边会多应该&符号 但不影响*/
                    })
                    this.deleteRequest("/system/institute/" + ids).then(res => {
                        if(res) {
                            /*刷新*/
                            this.initInstitute()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    }
</script>

<style>
    .addPosInput{
        width: 300px; margin-right: 8px
    }
    .posManaMain{
        margin-top: 10px;
    }
    .updatePosInput{
        width: 200px;
        margin-left: 8px;
    }
</style>
